<?php

namespace App\Http\Controllers;

use App\Good;
use App\Order;
use Godruoyi\Snowflake\Snowflake;
use Illuminate\Http\Request;
use Yansongda\Pay\Pay;

class OrderController extends Controller
{
    protected $config = [
        'app_id' => '2021000117606333',
        'notify_url' => 'http://yansongda.cn/notify.php',
        'return_url' => 'http://www.month2.com:8888/return',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtQjZhJvM+FfbtaUKkZkZo+rtTtNcCuZNqEVs1wdxaymv0o+UbNWo0HnzkzEiczLkFBQ/hK1045cRgFV+lNHS4wAz07R/XNOM8wyOuBf1ViqKB/947cSFnZJAk8vi5EbehzK5fygPeGpwzfINu7y4UlQBNTmN2jtB3XS69niiDoCj3DbfeJaiY9rdi85BwRWk75hv19zAlGtVOLVrMg0L0KNP2O6D260vx/Vv2uEkOsXO5wjOTxnsAkyKkpSDPW17xASXsJNkkxZ/gkkChvl6AcIuBN5kuO4BWTR3kziTEG/U1srMM10E4UeYCgnQvP55/8CvbJi0tzh3uO35h2ArYwIDAQAB',
        // 加密方式： **RSA2**
        'private_key' => 'MIIEpAIBAAKCAQEAlBdDsEh9AHqVUsJNe5Z8dmD3nALLfoYDCd6brDsU2gd/Ou9kIRLLqRAoCv7p1aEk30yLDlK8EipkoSqhq1mp0sz7WNePGuAOWklRNqMEJs3aLixzpldqkpe6If7PFGk1ADUpmFvlQfh8qFH3KTFd8gle9JML7ED3FvZTDgRnM5ZUmjCS/qjcn3a3QGT1VJNB0sokA6ddztyyhnDXuYnMUVHcLRvZF3FLkFtTx7cKWUzfTDgK74lLbLCNQnNIkcS03Q2ndys9SgRigrTa0m2Cfc6K6JuSckJsUtOyiKfYktXWAjiQugV+i0Z+59C5JXPn7qU/hEBWDz34WPUUUNFaYQIDAQABAoIBAFYJ3i3BL77jeXYHpXS0TcOJOtLNXOU5fvJw/wjlmovB502aYBZHEUQ8OseIqtqPMRj7I59Cujnjn2tzlVI0WdI7fZx0QJRgsHY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DbxDoqpj8g0b6diE/hWhDeOx/LjY3toWKYkLm4nkSX5Wy9fKSC1hHrWGgSde+XuFS0RA==',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];


    public function save(Request $request){
        $params = $request->input();
      $flakesnow = new Snowflake();
      $order_no = $flakesnow->id();
      $params['order_no'] = $order_no;
      $params['uid'] =1;
      if ($params['type'] == 2){
          $params['real_price'] = 200;
          $params['status'] = 3;
      }elseif ($params['type'] == 1){
          $params['real_price'] = $params['price'];
          $params['status'] = 3;
      }

      $model = Order::create($params);
      $data = json_decode(json_encode($model),true);
      $goods_id = $data['goods_id'];
      $goods = json_decode(json_encode(Good::find($goods_id)),true);
      return view('order',['model'=>$data,'goods'=>$goods]);
    }

    public function index(){
        $order = Order::all();
        return view('orders',['data'=>$order]);
    }

    public function detail(Request $request){
        $id = $request->input('id');
        $model = Order::find($id);
        $data = json_decode(json_encode($model),true);
        $goods_id = $data['goods_id'];
        $goods = json_decode(json_encode(Good::find($goods_id)),true);
        return view('order',['model'=>$data,'goods'=>$goods]);
    }

    public function passtime(Request $request){
        $alipay = Pay::alipay($this->config);
        $out_trade_no = $request->input('out_trade_no');
        $data = json_decode(json_encode(Order::where('order_no',$out_trade_no)->get()),true);
        Order::where('order_no',$out_trade_no)->update(['status'=>4]);
        $order = [
            'out_trade_no' => $out_trade_no,
            'refund_amount' => $data[0]['real_price'],
        ];
        $result = $alipay->refund($order);
        return $result;
    }



}
